public class Main {
    public static void main(String[] args) {
        ///字符串常见创建
        //1.
        String s1 = "hello!";
        System.out.println(s1);
        //2.
        String s2 = new String("hez");
        System.out.println(s2);
        //3.
        char[] array = {'h', 'e', 'l', 'l', 'o', '!'};
        String s3 = new String(array);
        System.out.println(s3);
        System.out.println(s1.compareTo(s2));

        ///字符串4种比较
        //1. ==

        //对于引用类型，比较地址
        String str1 = new String("abcd");
        String str2 = new String("abcd");

        //对于内置类型，比较内容
        String str3 = "abcd";
        String str4 = "abcd";

        System.out.println(str1 == str2);
        System.out.println(str3 == str4);

        //2. equals
        //只比较内容，返回false true
        String str5 = new String("abcd");
        String str6 = new String("abcd");
        String str7 = new String("aaaa");

        System.out.println(str5.equals(str6));
        System.out.println(str5.equals(str7));

        //3. compareTo
        //返回的是int型 大于返回大于0的 小于则负数
        System.out.println(str5.compareTo(str6));
        System.out.println(str5.compareTo(str7));


        //4. compareToIgnoreCase
        //不同于上面compareTo，它忽略大小写
        String str8 = new String("Abcd");
        System.out.println(str5.compareToIgnoreCase(str6));
        System.out.println(str5.compareToIgnoreCase(str8));//这俩结果一样

        System.out.println("============================");
        //字符串查找
        String arr1 = "abcda2bc1do1ab2oocdzbc2d";
        System.out.println(arr1.charAt(3));//查找下标为3的数 -> d
        System.out.println(arr1.indexOf('d'));//查找字符串中d的下标 -> 3
        System.out.println(arr1.indexOf('d', 8));//从下标8开始找含d的下标 ->9
        System.out.println(arr1.indexOf("ab"));//字符串ab，字符串中第一次出现的下标
        System.out.println(arr1.indexOf("ab", 8));//字符串ab，字符串中第一次出现的下标,从8下标开始找
        System.out.println(arr1.lastIndexOf('d'));//从尾向前开始找 类似indexOf后面的
        System.out.println(arr1.lastIndexOf('d', 5));
        System.out.println(arr1.lastIndexOf("ab"));
        System.out.println(arr1.lastIndexOf("ab", 5));


        //字符串转换
        //**1. 数值转换字符串————valueOf
        String t1 = String.valueOf(123);//int型
        String t2 = String.valueOf(12.3);//浮点型
        String t3 = String.valueOf(true);//boolean型

        System.out.println(t1);
        System.out.println(t2);
        System.out.println(t3);


        //*2. 字符串转数值
        int num1 = Integer.parseInt("123");
        double num2 = Double.parseDouble("123");

        System.out.println(num1);
        System.out.println(num2);


        //3. 大小写转换
        String r1 = "aaa";
        String r2 = "BBB";

        System.out.println(r1.toUpperCase());//转大写
        System.out.println(r2.toLowerCase());//转小写


        //4. 字符串转数组
        String aa = "hello";
        char[] bb = aa.toCharArray();
        for (int i = 0; i < bb.length; i++) {
            System.out.print(bb[i] + " ");
        }
        System.out.println();
        //*5.数组转字符串
        char[] ch = {'a', 'b', 'c', 'd'};
        String cc = new String(ch);
        System.out.println(cc);


        //6.去掉左右空格.
        //字符串中间的没去
        String rr1 = "       abc def          ";
        System.out.println("[" + rr1.trim() + "]");
        System.out.println("[" + rr1 + "]");

        //**7. 字符串格式化————format
        //错误！！！String sss = new String.format("%d-%d-%d",1999,4,14);
        String sss = String.format("%d-%d-%d", 1999, 4, 14);
        System.out.println(sss);


        //字符串替换
        String p = "aaaabbbbcccc";
        System.out.println(p.replace('a', 'o'));//将所有的字符a替换为o
        System.out.println(p.replaceAll("aa", "o"));//将所有的字符串aa替换为o
        System.out.println(p.replaceFirst("a", "oo"));//把出现的第一个字符串a替换为o


        //字符串拆分
        //以字符数组形式储存
        String q1 = "aaaa bbbb cccc";
        String[] array1 = q1.split(" ");
        for (int i = 0; i < array1.length; i++) {
            System.out.println(array1[i]);
        }

        String[] array2 = q1.split(" ", 2);//只能拆分成两组
        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i]);
        }
        //注意！！
        //1. 字符"|","*","+"都得加上转义字符，前面加上 "\\"
        //2. 而如果是 "\" ，那么就得写成 "\\\\"
        //3. 如果一个字符串中有多个分隔符，可以用"|"作为连字符


        //字符串截取
        System.out.println(arr1.substring(2));  //截取下标2到结尾范围的字符串
        System.out.println(arr1.substring(0, 4));  //截取下标[0,4)范围的字符串


        /*//题目错题
        public class SystemUtil{
            public static boolean isAdmin(String userId){
                return userId.toLowerCase()=="admin";//会返回”false“
                //不一样 左边等于会产生新的对象
            }
            public static void main(String[] args){
                System.out.println(isAdmin("Admin"));
            }
        }*/

        //oj题目1
        /*//找到第一个只出现一次出现的字符
        System.out.println("============================");
        int[] find = new int[26];
        String a1 = "abca";
        for (int i = 0; i < a1.length(); i++) {
            char cch = a1.charAt(i);
            find[cch - 'a']++;
        }
        for (int i = 0; i < find.length; i++) {
            if(find[i]==1){
                System.out.println(i);
                break;
            }
        }*/
        //错啦！这个是找出重复出现的
        /*String a1 = "abca";
        char[] a2 = a1.toCharArray();
        char tt = 'a';
        char[] a3 = new char[26];
        for (int i = 0; i < 26; i++) {
            a3[i] = tt;
            tt++;
        }
        int[] a4 = new int[26];
        for (int i = 0; i < a2.length; i++) {
            for (int j = 0; j < a3.length; j++) {
                if(a2[i]==a3[j]){
                    a4[j]++;
                }
            }
        }
        for (int i = 0; i < a4.length; i++) {
            if(a4[i]>1){
                System.out.println(a3[i]);
            }
        }*/



        /*//计算最后一个单词长度
        String a1 = "hello worlds";
        //找到最后一个空格的下标位置
        int k = a1.lastIndexOf(" ");
        //从k+1开始截取后面所有字符串
        String ret = a1.substring(k+1);
        //打印截取的字符串长度
        System.out.println(ret.length());
    }*/

        
    }
}